<?php 

// Custom fields for WP write panel

$pt_metaboxes = array(
		"fullimg" => array (
			"name"		=> "fullimg",
			"default" 	=> "",
			"label" 	=> __("Full Image (only for slider)"),
			"type" 		=> "text",
			"desc"      => __("Enter full image URL (image size: width - 978px x height - 400px) eg. : <strong>http://myplace.com/image.jpg</strong>",'templatic')
		),
		"video" => array (
			"name"  => "video",
			"label" => __("Custom Video code (only for slider)"),
			"type" => "textarea",
			"desc" => __("Enter embed code for video. (Ideal Size: width - 300px x height - 199px) eg. : code from youtube, vimeo, etc",'templatic')
		),
		
	);

// Custom fields for WP write panel
// This code is protected under Creative Commons License: http://creativecommons.org/licenses/by-nc-nd/3.0/

function ptthemes_meta_box_content() {
    global $post, $pt_metaboxes;
    $output = '';
    $output .= '<div class="pt_metaboxes_table">'."\n";
	$output .='<input type="hidden" name="templatic_meta_box_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';
	foreach ($pt_metaboxes as $pt_id => $pt_metabox) {
    if($pt_metabox['type'] == 'text' OR $pt_metabox['type'] == 'select' OR $pt_metabox['type'] == 'checkbox' OR $pt_metabox['type'] == 'textarea')
			$pt_metaboxvalue = get_post_meta($post->ID,$pt_metabox["name"],true);
           	if ($pt_metaboxvalue == "" || !isset($pt_metaboxvalue)) {
                $pt_metaboxvalue = $pt_metabox['default'];
            }
            if($pt_metabox['type'] == 'text'){
                $output .= "\t".'<div>';
                $output .= "\t\t".'<br/><p class=lable_title><label for="'.$pt_id.'">'.$pt_metabox['label'].'</label></p>'."\n";
                $output .= "\t\t".'<p><input size="100" class="pt_input_text" type="'.$pt_metabox['type'].'" value="'.$pt_metaboxvalue.'" name="ptthemes_'.$pt_metabox["name"].'" id="'.$pt_id.'"/></p>'."\n";
                $output .= "\t\t".'<p class=note>'.$pt_metabox['desc'].'</p>'."\n";
                $output .= "\t".'</div>'."\n";
            }
            elseif ($pt_metabox['type'] == 'textarea'){
				$output .= "\t".'<div>';
                $output .= "\t\t".'<br/><p class=lable_title><label for="'.$pt_id.'">'.$pt_metabox['label'].'</label></p>'."\n";
                $output .= "\t\t".'<p><textarea rows="5" cols="98" class="pt_input_textarea" name="ptthemes_'.$pt_metabox["name"].'" id="'.$pt_id.'">' . $pt_metaboxvalue . '</textarea></p>'."\n";
                $output .= "\t\t".'<p class=note>'.$pt_metabox['desc'].'</p>'."\n";
                $output .= "\t".'</div>'."\n";
            }
           elseif ($pt_metabox['type'] == 'select'){
                $output .= "\t".'<div>';
                $output .= "\t\t".'<br/><p class=lable_title><label for="'.$pt_id.'">'.$pt_metabox['label'].'</label></p>'."\n";
                $output .= "\t\t".'<p><select class="pt_input_select" id="'.$pt_id.'" name="ptthemes_'. $pt_metabox["name"] .'"></p>'."\n";
                $array = $pt_metabox['options'];
                if($array){
                    foreach ( $array as $id => $option ) {
                        $selected = '';
                        if($pt_metabox['default'] == $option){$selected = 'selected="selected"';} 
                        if($pt_metaboxvalue == $option){$selected = 'selected="selected"';}
                        $output .= '<option value="'. $option .'" '. $selected .'>' . $option .'</option>';
                    }
                }
                $output .= '</select><p class=note>'.$pt_metabox['desc'].'</p>'."\n";
                $output .= "\t".'</div>'."\n";
            }
            elseif ($pt_metabox['type'] == 'checkbox'){
                if($pt_metaboxvalue == '1') { $checked = 'checked="checked"';} else {$checked='';}
				$output .= "\t".'<div>';
              //  $output .= "\t\t".'<br/><p class=lable_title><label for="'.$pt_id.'">'.$pt_metabox['label'].'</label></p>'."\n";
                $output .= "\t\t".'<p><input type="checkbox" '.$checked.' class="pt_input_checkbox"  id="'.$pt_id.'" name="ptthemes_'. $pt_metabox["name"] .'" value="1" /> &nbsp;&nbsp; <label for="'.$pt_id.'">'.$pt_metabox['label'].'</label></p>'."\n";
                $output .= "\t\t".'<p class=note>'.$pt_metabox['desc'].'</p>'."\n";
                $output .= "\t".'</div>'."\n";
            }        
        }
    
    $output .= '</div>'."\n\n";
    echo $output;
}

function ptthemes_metabox_insert($post_id) {
    global $pt_metaboxes;
    global $globals;
	
	// verify nonce
    if (!wp_verify_nonce($_POST['templatic_meta_box_nonce'], basename(__FILE__))) {
        return $post_id;
    }

    // check autosave
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return $post_id;
    }
	
    $pID = $_POST['post_ID'];
    $counter = 0;
    foreach ($pt_metaboxes as $pt_metabox) { // On Save.. this gets looped in the header response and saves the values submitted
    if($pt_metabox['type'] == 'text' OR $pt_metabox['type'] == 'select' OR $pt_metabox['type'] == 'checkbox' OR $pt_metabox['type'] == 'textarea') // Normal Type Things...
        {
            $var = "ptthemes_".$pt_metabox["name"];
			 if (isset($_POST[$var])) {            
                if( get_post_meta( $pID, $pt_metabox["name"] ) == "" )
                    add_post_meta($pID, $pt_metabox["name"], $_POST[$var], true );
                elseif($_POST[$var] != get_post_meta($pID, $pt_metabox["name"], true))
                    update_post_meta($pID, $pt_metabox["name"], $_POST[$var]);
            }
			elseif($_POST[$var] == "")
                    delete_post_meta($pID, $pt_metabox["name"], get_post_meta($pID, $pt_metabox["name"], true));
			
        } 
    }
}


function ptthemes_meta_box() {
    if ( function_exists('add_meta_box') ) {
        add_meta_box('ptthemes-settings',$GLOBALS['themename'].'Custom Settings','ptthemes_meta_box_content','post','normal','high');
    }
}

add_action('admin_menu', 'ptthemes_meta_box');
add_action('save_post', 'ptthemes_metabox_insert');

?>